<template>
  <!-- 表单 -->
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="hFocus" />
      <div v-if="showTree" class="tree-box">
        <el-tree :data="treeList" :props="{ label: 'name' }" @node-click="hNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeesEnum from '@/constant/employees' // 引入常量
import { getDepartments } from '@/api/departments' // 导入接口函数
import { tranListToTreeData } from '@/utils' // 引入树形结构转换方法
import { addEmployee } from '@/api/employees'
export default {
  name: 'EmployAdd',
  data() {
    return {
      hireType: EmployeesEnum.hireType, // 正式非正式常量数据
      // 保存表单数据的对象
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 验证规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      treeList: [], // 保存树形数组
      showTree: false // 控制表单里部门信息的显示隐藏
    }
  },
  methods: {
    // 取消
    hCancel() {
      this.$emit('close')
    },
    // 确定 -> 兜底校验
    hSubmit() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          // 实现添加
          this.doAdd()
        }
      })
    },
    // 获取部门列表 -> 导入departments.js里的获取部门列表接口函数 -> 发送请求
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('获取部门列表', res)
        res.data.depts.shift() // 第一条数据是公司而不是部门 -> shift会取出数组第一个元素，同时会改变源数组
        this.treeList = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log('获取部门列表失败', error)
      }
    },
    // 表单获得焦点 -> 请求数据
    hFocus() {
      this.loadDepartments()
      this.showTree = true // 显示
    },
    // 用户在部门结构树上点击
    hNodeClick(data, node) {
      console.log('用户在树形数据上点击', data, node)
      console.log('用户在树形数据上选中的是', data.name)
      if (data.children.length) return // 如果有子集 点击无效
      this.formData.departmentName = data.name // 将这个数据给到表单的departmentName
      this.showTree = false // 树形数据隐藏
    },
    // 实现新增 -> 关闭弹窗 刷新页面
    async doAdd() {
      try {
        const res = await addEmployee(this.formData)
        console.log('新增员工成功', res)
        this.$message.success('新增成功')
        this.$emit('success')
      } catch (error) {
        console.log('新增员工失败,原因是', error)
      }
    }
  }
}
</script>
